#########################################
#Getting data, params, etc. from BLP ====
#########################################
library(HeadR)
#
source(file = "Rfiles/CF_BLPdata_brandiso.R")
#
# Read original BLP data
BLP <- fread("Data/BLP99/blp_1999_data.csv")
fwrite(BLP[,.(car_ids=car_id,domestic=domestic)],"Data/BLP99/domestic.csv")
#car_id is the unique identifier 
BLP[,firm_ab := substr(model_id,1,2)] # this is actually brand, so do not use it !
BLP[year==90,lapply(.SD,uniqueN),.SDcols =c("model_id","car_id","firm_id","firm_ab")]
setnames(BLP,old=c("model_id","firm_id","price","share","quantity"),new=c("m","f","p","s","q"))
BLP[, year := year+1900]
BLP[, n := 1]
BLP[, i := domestic]
BLP <- BLP[,.(year,m,f,p,s,q,const,hpwt,air,mpd,space,domestic,i,n)]
#getting back brand origin
BLP[, b := substr(m,1,2)] # brands
BLP <- merge_stata(BLP,brands,by="b")
BLP[, table(year,stata_merge)] # 1990 is complete
BLP[, stata_merge := NULL]
BLP[, target := iso %in% target.list]
#
#
#following file needs to a) be created by read_pyblp_output.R b) conform to same structure as published_param.csv
param99 <- fread("Data/BLP99/published_param.csv") # BLP1995 parameters
param <- fread("Data/BLP99/pyblp_domestic_param.csv") # estimated using C&G package
param[, par_abbrev := sub(pattern = "cons", replacement = "const",par_abbrev)]
param[, par_abbrev := sub(pattern = "s_", replacement = "sigma_",par_abbrev)]
param[, par_abbrev := sub(pattern = "b_price", replacement = "alpha_price",par_abbrev)]
param[, par_abbrev := sub(pattern = "b_", replacement = "demand_",par_abbrev)]
param[, par_abbrev := sub(pattern = "g_", replacement = "supply_",par_abbrev)]
setnames(param,old=c("par_abbrev"), new=c("param_name"))
param[param_name=="alpha_price" , (2:6) :=  lapply(.SD,function(x) x =-x), .SDcols=-1]
param_merge <- merge_stata(param99,param,by="param_name")
param_merge[,Domestic_pub_param := pub_param]
param_merge[param_name=="demand_domestic",Domestic_pub_param := 0.85] #RV 2014 (Table 6, opt IV, column ii)
param_merge[param_name=="sigma_domestic",Domestic_pub_param := sigmadomesticpct/100] #RV 2014 (Table 6, opt IV, column i)
param_merge[order(stata_merge)]
param_merge[, char.num := fcase(param_name %like% "const",0,
                                param_name %like% "hpwt",1,
                                param_name %like% "air",2,
                                param_name %like% "mpd",3,
                                param_name %like% "space",4,
                                param_name %like% "domestic",5, default =  6 )]
param_merge[, est.num := fcase(param_name %like% "sigma",0,
                                param_name %like% "alpha",1,
                                param_name %like% "demand",2,
                                param_name %like% "supply",3)]
setorder(param_merge,est.num,char.num)
#Here choose which set of parameters
chosen.var <- param.chosen
param <- param_merge[, .(param_name, pub_param=get(chosen.var))]
#
#Load parameters in the environment
param[, xvar := HeadR::st_right(param_name,"_")]
alpha <- param[param_name=="alpha_price"]$pub_param
b.means <- param[param_name %like% "demand"]$pub_param
names(b.means) <- param[param_name %like% "demand"]$xvar
b.sds <- param[param_name %like% "sigma"]$pub_param
names(b.sds) <- param[param_name %like% "sigma"]$xvar
y.mn <- fread("Data/BLP99/meanincome.csv")
y.mn[, year := V1+1900]
y.sd <- fread("Data/BLP99/sdincome.csv")$V1
